var app = getApp();
var server = require('../../utils/server');
Page({
	data: {
		img_url: 'http://17fx.com/imgs/',
		filterId: 1,
		searchWords: '',
		placeholder: '输入商家名称',
		shops: app.globalData.shops,
    list:[],
    page:1
	},
	onLoad: function () {
    var that = this;
    Getglist(that, '', getApp().globalData.city_id, 1);
	},
	onShow: function () {
		this.setData({
			showResult: false
		});
	},
	inputSearch: function (e) {
		this.setData({
			searchWords: e.detail.value
		});
    
	},

  formSubmit: function(e) {
    console.log('搜索字段' + e.detail.value['sousuo']);
		this.setData({
      searchWords: e.detail.value['sousuo']
		});
    var that = this;
    //清空集合
    that.setData({
      list: [],
      page:1
    })
    //调用getlist方法加载排行集合
    Getglist(that, e.detail.value['sousuo'], getApp().globalData.city_id,1);
    console.log('定位' + getApp().globalData.city_id);
	},
  //上拉加载数据绑定
  onReachBottom: function (event) {
    console.log("上拉加载数据绑定")
    var that = this
    
    //调用getlist方法加载排行集合
    Getglist(that, '', getApp().globalData.city_id,that.data.page)
  },
  //下拉刷新
  onPullDownRefresh: function () {
    //调用getlist方法获取美女排行榜集合
    var that = this


  }

});

var url = "https://17fx.com/weapp/api/getshoplist.html";
//

var page_size = 10;
var Getglist = function (that,words,city_id,page) {
  wx.request({
    url: url,
    data: {
      openid: getApp().globalData.openid,
      q:words,
      city_id:city_id,
      page: page,
      page_size: page_size
    },
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      console.log(res.data);
      if(that.data.page == 1){
        that.setData({
          list:[]
        })
      }
      if(res.data != null ){
        var list = that.data.list;
        for (var i = 0; i < res.data.length; i++) {
          list.push(res.data[i]);
        }
        that.setData({
          list: list,
          page:that.data.page+1
        });
      }else{
        if(that.data.page == 1){
          wx.showModal({
            title: '提示',
            content: '没有搜索到数据',
            showCancel:false,
            success: function (res) {

            }
          })
        }else{
          wx.showToast({
            title: '加载到底了',
            icon: 'success',
            duration: 3000
          })
        }

      }

    }
  })
}